<?php
/**
 * Created by PhpStorm.
 * User: duanshuai
 * Date: 2018/6/19
 * Time: 下午4:37
 */

namespace app\index\controller;

use app\index\model\MonthBill;
use think\Db;
use think\request;
use app\index\model;
class Bill extends Base
{

    //增加账单
    public function recordDailyBill(){
        $request = request()->param();
        $data = [];
        $result = $this->checkParamsWithToken($request,['cost']);



        if ($result['result']){

            unset($result['result']);
            $model = new model\DailyBill();
            $model->addBill($result);
            $model_mon = new MonthBill();
            $model_mon->updateMonthBill($result['count'],$result['cost']);


            if (!empty($request['type_id'])) {

                //将type表cost数据增加
                $type_model = new model\TypeBill();

                $type_model->where('id',$request['type_id'])->setInc('cost',$request['cost']);


            }



            $data = $this->dataFormate_0('增加成功');

        }else{
            $data = $this->dataFormate_1('认证失败',$result);
        }

        return $data;

    }

    /*
     * 获取个人总账单   按照时间顺序
     * */
    public function getMyBill(){

        $request = request()->param();
        $data = [];

        $chek = $this->checkParamsWithToken($request,['page']);
        if ($chek['result']){
            unset($chek['result']);

            $bill = new model\DailyBill();

            $list = $bill->findCountBill($chek);

            $data = ['status'=>0,'statusCode'=>'成功','data'=>$list];



        }else{
            $data = $this->dataFormate_1('认证失败',$chek);
        }





        return $data;

    }


    /*
     * 获取账单月份列表 一级目录
     * */
    /**
     * @return array
     */
    public function getMonthList(){

        $request = request()->param();
        $data = [];

        $chek = $this->checkParamsWithToken($request,['page']);
        if ($chek['result']){

            $bill = new MonthBill();
            $list = $bill->findMonthBill($chek['count'],$chek['page']);

            $data = $this->dataFormateList_0('成功',$list['data'],$list['total']);


        }else{
            $data = $this->dataFormate_1('认证失败',$chek);
        }

        return $data;

    }

    /**
     * Notes:根据年份 获取每个月的支出情况
     * User: 段帅
     * Time: 2019/6/11  1:46 PM(摁tab)
     * @return array
     */
    public function getYearList(){

        $request = request()->param();
        $data = [];

        $chek = $this->checkParamsWithToken($request,['year']);
        if ($chek['result']){

            $bill = new MonthBill();
            $list = $bill->findYearBill($chek['count'],$chek['year']);

            $data = $this->dataFormate_0('成功',$list['data']);


        }else{
            $data = $this->dataFormate_1('认证失败',$chek);
        }

        return $data;

    }


    /*
     * 每月个人详细花费  二级目录 time : 2018-06
     * */
    public function getMonthCount(){
        $request = request()->param();
        $data = [];
        $chek = $this -> checkParamsWithToken($request,['time','page']);
        if ($chek['result']){
            $bill = new model\DailyBill();
            $list = $bill->findMonthBill($chek);
            $data = $this->dataFormateList_0('成功',$list['data'],$list['total']);
        }else{
            $data = $this->dataFormate_1('认证失败',$chek);
        }

        return $data;



    }

    /*
     * 同步个人month 数据
     * */
    public function updateMonthList(){

        $request = request()->param();
        $data = [];
        $chek = $this -> checkParamsWithToken($request);
        if ($chek['result']){
            $mon_bill = new MonthBill();
            $mon_bill -> updateMonth_tongbu($chek['count']);
            $data =  $this->dataFormate_0('成功');
        }else{
            $data = $this->dataFormate_1('认证失败',$chek);
        }

        return $data;

    }

    /**
     * Notes:更新bill
     * User: 段帅
     * Time: 2018/10/18  下午2:58
     * @return array
     */
    public function updateBill(){
        $requset = request()->param();
        $data = [];
        $chek = $this -> checkParamsWithToken($requset,['bill_id']);
        if ($chek['result']){
            $id =$chek['bill_id'];
            $old_data = model\DailyBill::get($id);
            $type_id = $old_data -> type_id;
            $cost_old = $old_data -> cost;
            $bill = new model\DailyBill();
            $bill->allowField(true)->save($chek,['id' => $id]);



            if (!empty($type_id)) {

                $old_data -> typebill -> setInc('cost',$chek['cost']-$cost_old);

            }




            $data = $this->dataFormate_0('更新成功');

        }else{

            $data = $this->dataFormate_1('认证失败',$chek);
        }
        return $data;
    }

    /**
     * Notes: 删除bill
     * User: 段帅
     * Time: 2018/10/18  下午3:04
     * @return array
     */
    public function deleBill(){
        $request = request()->param();
        $data = [];
        $chek = $this->checkParamsWithToken($request,['bill_id']);
        if ($chek['result']){

            $id =$chek['bill_id'];
            $bill =  model\DailyBill::get($id);
            if (empty($bill)){
                $data = $this->dataFormate_1('没有找到要删除的数据',$chek);
            }else{

                if (!empty($bill->type_id)) {
                    $bill-> typebill -> setInc('cost',-$bill->cost);

                }

                $bill -> delete();
                $data = $this->dataFormate_0('删除成功');
            }




        }else{
            $data = $this->dataFormate_1('认证失败',$chek);
        }
        return $data;
    }


    /**
     * Notes: 增加账单分类
     * User: 段帅
     * Time: 2018/10/23  下午5:11(摁tab)
     * @return array
     */
    public function addTypeBill(){

        $request = request()->param();
        $data = [];
        $chek = $this->checkParamsWithToken($request,['title']);
        if ($chek['result']){
            unset($chek['result']);
            $type = new model\TypeBill();
            $type->save($chek);
            $data = $this->dataFormate_0('增加成功');

        }else{
            $data = $this->dataFormate_1('认证失败',$chek);
        }
        return $data;

    }

    /**
     * Notes: 删除账单分类
     * User: 段帅
     * Time: 2018/10/23  下午5:11(摁tab)
     * @return array
     */
    public function deleTypeBill(){

        $request = request()->param();
        $data = [];
        $chek = $this->checkParamsWithToken($request,['type_id']);
        if ($chek['result']){

            $type =  model\TypeBill::get($chek['type_id']);
            if (empty($type)) {
                $data = $this->dataFormate_1("没有要删除的信息");
            }else{
                $type-> status = 1;
                $type->save();

                $data = $this->dataFormate_0('删除成功');
            }



        }else{
            $data = $this->dataFormate_1('认证失败',$chek);
        }
        return $data;

    }

    /**
     * Notes: 修改账单分类
     * User: 段帅
     * Time: 2018/10/23  下午5:11(摁tab)
     * @return array
     */
    public function updateTypeBill(){

        $request = request()->param();
        $data = [];
        $chek = $this->checkParamsWithToken($request,['type_id','title']);
        if ($chek['result']){

            $type =  model\TypeBill::get($chek['type_id']);
            if (empty($type)) {
                $data = $this->dataFormate_1("没有要修改的信息");
            }else{
                $type-> title = $chek['title'];
                $type->save();

                $data = $this->dataFormate_0('修改成功');
            }



        }else{
            $data = $this->dataFormate_1('认证失败',$chek);
        }
        return $data;

    }



    /**
     * Notes: 查找账单分类
     * User: 段帅
     * Time: 2018/10/23  下午5:11(摁tab)
     * @return array
     */
    public function findTypeBill(){

        $request = request()->param();
        $data = [];
        $chek = $this->checkParamsWithToken($request);
        if ($chek['result']){
            unset($chek['result']);
            $mod = new model\TypeBill();
            //如果没有page参数,返回全部,如果有 返回分页
            if (empty($chek['page'])) {
                $list = $mod->where(['count'=>$chek['count'],'status'=>0])->order('id','desc')->select();
                if ($list) {
                    $list = collection($list)->toArray();
                }

                $data = $this->dataFormate_0('查询成功',$list);
            }else{
                $page = $chek['page'];

                $list = $mod -> where(['count'=>$chek['count'],'status'=>0])->order('create_time','desc') -> limit(10*($page-1),10*$page)->field('id,cost,create_time,title')-> select();
                $total =$mod -> where(['count'=>$chek['count'],'status'=>0]) -> count();

                if ($list) {
                    $list = collection($list)->toArray();
                }

                $data = $this->dataFormateList_0('成功',$list,$total);


            }


        }else{
            $data = $this->dataFormate_1('认证失败',$chek);
        }
        return $data;

    }


    /*
    * 每个专题的详细花费 time : 2018-06
    * */
    public function getTypeCount(){
        $request = request()->param();
        $data = [];
        $chek = $this -> checkParamsWithToken($request,['type_id','page']);
        if ($chek['result']){
            $bill = new model\DailyBill();
            $list = $bill->findTypeBill($chek);
            $data = $this->dataFormateList_0('成功',$list['data'],$list['total']);
        }else{
            $data = $this->dataFormate_1('认证失败',$chek);
        }

        return $data;



    }




}